package cn.dankal.flyaudio.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.dankal.basiclib.base.recyclerview.BaseListAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewAdapter;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewHolder;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.CreateOrderPreviewEntity;
import cn.dankal.basiclib.pojo.ProductDetailItemEntity;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.OrderBean;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.personal.MyOrderActivity;
import cn.dankal.flyaudio.personal.MyOrderDetailActivity;

public class OrderAdapter extends BaseRecyclerViewAdapter<CreateOrderPreviewEntity> {

    private OnButtonClickListener listener;

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.item_order;
    }

    public void setOnButtonClickListener(OnButtonClickListener listener) {
        this.listener = listener;
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(ViewGroup parent, View rootView, int viewType) {
        return new ViewHolder(rootView);
    }

    class ViewHolder extends BaseRecyclerViewHolder<CreateOrderPreviewEntity> {

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onBindData(CreateOrderPreviewEntity data, int position) {
            RecyclerView list = itemView.findViewById(R.id.rv_products);
            if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_PAY)) {
                setText(R.id.tv_order_state, "待付款");
                setText(R.id.button, "付款");
                setVisibility(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.TIMEOUT_CANCELLED)) {
                setText(R.id.tv_order_state, "已取消");
                setGone(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_SHIP)) {
                setText(R.id.tv_order_state, "待发货");
                setGone(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.SHIPPING)) {
                setText(R.id.tv_order_state, "待收货");
                setText(R.id.button, "确认收货");
                setVisibility(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_COMMENT)) {
                setText(R.id.tv_order_state, "交易成功");
                setText(R.id.button, "评价");
                setVisibility(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.SUCCESS)) {
                setText(R.id.tv_order_state, "交易成功");
                setGone(R.id.button, true);

            } else if (TextUtils.equals(data.getState(), MyOrderActivity.CLOSE)) {
                setText(R.id.tv_order_state, "交易关闭");
                setGone(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.AFTER_SALE)) {
                setText(R.id.tv_order_state, "退款/售后");
                setGone(R.id.button, true);
            } else if (TextUtils.equals(data.getState(), MyOrderActivity.USER_CANCELLED)) {
                setText(R.id.tv_order_state, "已取消");
                setGone(R.id.button, true);
            }
            int count = 0;
            List<ProductDetailItemEntity> order = data.getOrderItemList();
            for (ProductDetailItemEntity entity : order) {
                count += entity.getItemCount();
            }
           TextView button =  findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        if("付款".equals(button.getText().toString())){
                            listener.doPay(data);
                        }else if("确认收货".equals(button.getText().toString())){
                            listener.confirmReceive(data);
                        }else if("评价".equals(button.getText().toString())){
                            listener.toComment(data);
                        }
                    }
                }
            });
            setText(R.id.tv_order_count, "共" + count + "件，合计 ¥");
            setText(R.id.tv_order_amount, data.getNeedPay()+"");
            list.setLayoutManager(new LinearLayoutManager(getContext()));
            ProductsAdapter adapter = new ProductsAdapter();
            adapter.setOnRvItemClickListener(new OnRvItemClickListener<ProductDetailItemEntity>() {
                @Override
                public void onItemClick(View v, int position, ProductDetailItemEntity data) {
                    ToastUtils.showShort("点击");
                }
            });
            adapter.setOnRvItemClickListener(new OnRvItemClickListener<ProductDetailItemEntity>() {
                @Override
                public void onItemClick(View v, int position, ProductDetailItemEntity pData) {
                    Intent intent = new Intent(ActivityUtils.getTopActivity(), MyOrderDetailActivity.class);
                    intent.putExtra("orderUuid", data.getUuid());
                    ActivityUtils.getTopActivity().startActivity(intent);
                }
            });
            list.setAdapter(adapter);
            adapter.addDatas(data.getOrderItemList(), true);
        }
    }

    public interface OnButtonClickListener {

        void doPay(CreateOrderPreviewEntity createOrderPreviewEntity);

        void confirmReceive(CreateOrderPreviewEntity createOrderPreviewEntity);

        void toComment(CreateOrderPreviewEntity createOrderPreviewEntity);
    }
}
